﻿using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web.Mvc;
using BlogPlat.Model;
using BlogPlat.Areas.Admin;
using BlogPlat.Areas.Admin.Controllers;
using AdminTagController = BlogPlat.Areas.Admin.Controllers.TagController;
using BlogPlat.Tests.Models;
using System.Text.RegularExpressions;
using BlogPlat.Tests.Helpers;

namespace BlogPlat.Tests.Areas.Admin.Controllers
{
    /// <summary>
    /// Summary description for AdminTagControllerTest
    /// </summary>
    [TestClass]
    public class AdminTagControllerTest
    {
        public AdminTagControllerTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void Admin_Can_Create_New_Tag()
        {
            // Arrange
            FakeTagRepository repo = new FakeTagRepository();
            TagController controller = new TagController(repo);

            // Act
            Tag newTag = new Tag { TagID = 4, TagName = "New Tag", FriendlyUrl = "new-tag" };
            controller.Create(newTag);
            var result = (ViewResult)controller.Index();
            var firstEntry = (Tag)((IList<Tag>)result.ViewData.Model)[0];

            // Assert
            Assert.AreEqual(firstEntry, newTag);
        }

        [TestMethod]
        public void Admin_TagController_Index_Action_Returns_All_Tags()
        {
            // Arrange
            FakeTagRepository repo = new FakeTagRepository();
            List<Tag> tags = Mock.CreateDummyTagData();
            foreach (Tag t in tags)
                repo.Add(t);
            var adminTagController = new AdminTagController(repo);

            // Act
            var result = (ViewResult)adminTagController.Index();

            // Assert
            CollectionAssert.AllItemsAreInstancesOfType((ICollection)result.ViewData.Model, typeof(Tag)); 
        }

        [TestMethod]
        public void Admin_Can_Delete_Tag()
        {
            // Arrange
            var fakeTagRepository = new FakeTagRepository();
            var controller = new AdminTagController(fakeTagRepository);
            var tags = Mock.CreateDummyTagData();
            var tag = tags[2];

            // Act
            foreach (Tag t in tags)
                controller.Create(t);

            controller.Delete(tag.TagID);

            // Assert
            Assert.IsNull(fakeTagRepository.Get(tag.TagID));
        }

        [TestMethod]
        public void Tag_Cannot_Be_Deleted_If_Connected_To_Posts()
        {

        }

        [TestMethod]
        public void Admin_Can_Update_Tag()
        {

        }
    }
}
